<?php

//Site application root and paths
//error_reporting(0);
define('APP_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);
define('BASE_PATH', dirname(__file__));

// Define path to application directory
defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));

// Define application environment
defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
define('APPLICATION_PATH_ADMIN', realpath(dirname(__FILE__) . '/application/module/admin'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(APPLICATION_PATH . '\models',
            realpath(APPLICATION_PATH . '/../library'),
            get_include_path(),
        )));


// Zend_Application
require_once 'Zend/Application.php';
require_once 'Zend/Loader/Autoloader.php';

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace(array('GTL_'));
$autoloader->setFallbackAutoloader(true);
$autoloader->suppressNotFoundWarnings(false);
// Create instance for registry
$registry = Zend_Registry::getInstance();

// Manipulating Configuration Data with Zend_Config and assign in registry
$sections[] = "production";
//$sections[] = "development";
$config = new Zend_Config_Ini(APPLICATION_PATH . DIRECTORY_SEPARATOR . 'configs' . DIRECTORY_SEPARATOR . 'application.ini', $sections);
$registry->config = $config;
//$registry->domainName = $config->domain_name;
//date_default_timezone_set($config->timezone);
$db = Zend_Db::factory($config->resources->db);
$db->setFetchMode(Zend_Db::FETCH_ASSOC);
$registry->db = $db;
Zend_Db_Table::setDefaultAdapter($db);
$moduleLoader = new Zend_Application_Module_Autoloader(
                array(
                    'namespace' => '',
                    'basePath' => APPLICATION_PATH
        ));
$moduleLoader->addResourceType('model', 'models', 'Model_');


// Intialize zend log
/*  Error logs */
/*
if ($config->log->errorLogging == true) {
    $logPath = $config->log->logBasePath . 'cli_error.log';
    if (!file_exists($logPath))
        touch($logPath);
    $writerLog = new Zend_Log_Writer_Stream($logPath);
    $writerLog->addFilter(new Zend_Log_Filter_Priority(Zend_Log::INFO, '<')); //will only log entries to this log that are < INFO, ZEND Specific
    $logerr = new GTL_Log($writerLog);
    $registry->logerr = $logerr;
}
 *
 * */


try {
    $opts = new Zend_Console_Getopt(
                    array(
                        'help|h' => 'Displays usage information.',
                        'action|a=s' => 'Action to perform in format of module.controller.action',
                        'verbose|v' => 'Verbose messages will be dumped to the default output.',
                        'development|d' => 'Enables development mode.',
                    )
    );
    $opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
    exit($e->getMessage() . "\n\n" . $e->getUsageMessage());
}


if (isset($opts->h)) {
    echo $opts->getUsageMessage();
    exit;
}
// option is -a
if (isset($opts->a)) {
    global $commandlineargs;
    $reqRoute = array_reverse(explode('.', $opts->a));
    @list($action, $controller, $module) = $reqRoute;
    $request = new Zend_Controller_Request_Simple($action, $controller, $module, $opts->getRemainingArgs());
    $commandlineargs = $opts->getRemainingArgs();

    // Initialize and set the front controller options
    try {
        $front = Zend_Controller_Front::getInstance();
        $front->addModuleDirectory(APPLICATION_PATH . '/modules/cli/');
        $front->setControllerDirectory(APPLICATION_PATH . "/modules/cli/controllers");
        $front->setRequest($request);
        $front->setRouter(new GTL_Cli());
        $front->setResponse(new Zend_Controller_Response_Cli());
        try {
            $front->dispatch();
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}else if (isset($opts->v)) {
    echo "Hello Script is working fine bye bye...";
}
/**
 * Function to check the values
 * @param array $ary
 * @param bool $exit
 */
function pr($ary = "", $exit = 0) {
    echo "<PRE>";
    print_r($ary);
    echo "</PRE>";
    if ($exit)
        exit();
}

/*Command to run form cmd or the shell access for running the cron job
 * php D:\wamp\www\photobook\zfcli.php -a index.index
 */